(function($) {
	var Paged = function(ele, options) {
		this.ele = ele;
		this.img = $("<img src=\"/lccrm/Content/paged/images/loadingPng.gif\" />");
		this.mask = $("<div/>");
		this.defaults = {
			pageCount: 10,
			current: 1,
			beginload: false,
			data: {},
			clickPage: function() {}
		}
		this.options = $.extend(this.defaults, options);
	}
	Paged.prototype = {
		initPaged: function() {
			this.LoadHtml(this.ele, this.options);
			this.LoadEvent(this.ele, this.options);
			if (this.options.beginload) {
				this.ChangeHtml(this.ele,this.options,1);
			}
		},
		LoadHtml: function(obj, args) {
			return (function() {
				obj.empty();
				if (args.current > 1) {
					obj.append('<a href="javascript:;" class="prevPage"><<</a>');
				} else {
					obj.remove('.prevPage');
					obj.append('<span class="disabled"><<</span>');
				}
				if (args.current != 1 && args.current >= 4 && args.pageCount != 4) {
					obj.append('<a href="javascript:;" class="tcdNumber">' + 1 + '</a>');
				}
				if (args.current - 2 > 2 && args.current <= args.pageCount && args.pageCount > 5) {
					obj.append('<span>...</span>');
				}
				var start = args.current - 2,
					end = args.current + 2;
				if ((start > 1 && args.current < 4) || args.current == 1) {
					end++;
				}
				if (args.current > args.pageCount - 4 && args.current >= args.pageCount) {
					start--;
				}
				for (; start <= end; start++) {
					if (start <= args.pageCount && start >= 1) {
						if (start != args.current) {
							obj.append('<a href="javascript:;" class="tcdNumber">' + start + '</a>');
						} else {
							obj.append('<span class="current">' + start + '</span>');
						}
					}
				}
				if (args.current + 2 < args.pageCount - 1 && args.current >= 1 && args.pageCount > 4) {
					obj.append('<span>...</span>');
				}
				if (args.current != args.pageCount && args.current < args.pageCount - 2 && args.pageCount != 4) {
					obj.append('<a href="javascript:;" class="tcdNumber">' + args.pageCount + '</a>');
				}
				if (args.current < args.pageCount) {
					obj.append('<a href="javascript:;" class="nextPage">>></a>');
				} else {
					obj.remove('.nextPage');
					obj.append('<span class="disabled">>></span>');
				}
			})();
		},
		ChangeHtml: function(obj, args, current) {
			var _This = this;
			_This.LoadMask();		
			var complete = args.complete;
			args.complete = function(httpRequest, status) {
				_This.CloseMask();
				_This.LoadHtml(obj, {
					"current": current,
					"pageCount": args.pageCount
				});
				if (typeof(args.clickPage) == "function") {
					args.clickPage(current);
				}
			};
			$.extend(args.data, {
			    currentPage: current
			});
			args.async = true;
			$.ajax(args);

		},
		LoadEvent: function(obj, args) {
			var _This = this;
			return (function () {
			    obj.off("click", "a.tcdNumber");
				obj.on("click", "a.tcdNumber", function() {
					var current = parseInt($(this).text());
					_This.ChangeHtml(obj, args, current);
				});
				obj.off("click", "a.prevPage");
				obj.on("click", "a.prevPage", function() {
					var current = parseInt(obj.children("span.current").text());
					_This.ChangeHtml(obj, args, current - 1);
				});
				obj.off("click", "a.nextPage");
				obj.on("click", "a.nextPage", function() {
					var current = parseInt(obj.children("span.current").text());
					_This.ChangeHtml(obj, args, current + 1);
				});
			})();
		},
		LoadMask: function() {
			var Limg = this.img;
			var Lmask = this.mask;
			$("body").append(Limg).append(Lmask);
			Limg.css({
				"z-index": "2000",
				"display": "none"
			})
			Lmask.css({
				"position": "fixed",
				"top": "0",
				"right": "0",
				"bottom": "0",
				"left": "0",
				"z-index": "1000",
				"background-color": "#000000",
				"display": "none"
			});
			Limg.show().css({
				"position": "fixed",
				"top": "50%",
				"left": "50%",
				"margin-top": function() {
					return -1 * Limg.height() / 2;
				},
				"margin-left": function() {
					return -1 * Limg.width() / 2;
				}
			});
			Lmask.show().css("opacity", "0.1");
		},
		CloseMask: function() {
			this.img.hide();
			this.mask.hide();
		}
	};
	$.fn.PagedInfo = function(options) {
	    var paged = new Paged(this, options);
	    if (options == null) {
	        return null;
	    }
		return paged.initPaged();
	}
})(jQuery)